// Copyright (c) 2012, 2013 All Right Reserved, http://hpc.utp.edu.my
// Author: Nordin Zakaria nordinzakaria@gmail.com
// All rights reserved.

#include "JobAssigned.h"

void JobAssigned::copy(const JobAssigned& orig)
 {
  if(&orig == this) return;

  initM = orig.initM;
  id = orig.id;

  unsigned int sz = orig.properties_size();
  properties_size(sz); 
  for (unsigned int i=0; i<sz; i++) {
	set(orig.get(i), i);	
	}
 }

void JobAssigned::read(FILE *infile)
   {
    unsigned int id;
    fread(&id,sizeof(unsigned int),1,infile);
    setID(id);

    unsigned int numprop;
    fread(&numprop,sizeof(unsigned int),1,infile);
    properties_size(numprop);
    for (unsigned int i=0; i<numprop; i++)
	{
    	unsigned int prop;
    	fread(&prop,sizeof(unsigned int),1,infile);
	set(prop, i);
	}
   }

void JobAssigned::write(FILE *outfile)
    {
     fwrite(&id, sizeof(unsigned int), 1, outfile);

     unsigned int numprop =  properties.size();
     fwrite(&numprop, sizeof(unsigned int), 1, outfile);
     for (unsigned int i=0; i<numprop; i++)
	{
    	unsigned int prop = get(i);
        fwrite(&prop, sizeof(unsigned int), 1, outfile);
	}
    }

ostream& operator<<(ostream& os, const JobAssigned& obj) 
   {
    if (os == cout) {
    	unsigned int numprop =  obj.properties.size();
	cout << "Job id: " << obj.id << ", numprop= " << numprop << endl << "\t props: ";
    	for (unsigned int i=0; i<numprop; i++)
		{
    		unsigned int prop = obj.get(i);
		cout << prop << " ";
		}
	cout << endl;
	return os;
	}

    os.write ((char*)&obj.id, sizeof (unsigned int));

    unsigned int numprop =  obj.properties.size();
    os.write ((char*)&numprop, sizeof (unsigned int));
    for (unsigned int i=0; i<numprop; i++)
	{
    	unsigned int prop = obj.get(i);
    	os.write ((char*)&prop, sizeof (unsigned int));
	}
    return os;
   }

istream& operator>>(istream& is, JobAssigned& obj)
   {
    char buffer[128];
    unsigned int id;
    is.read(buffer, sizeof(unsigned int));
    id = atoi(buffer);
    obj.setID(id);

    unsigned int numprop;
    is.read(buffer, sizeof(unsigned int));
    numprop = atoi(buffer);
    obj.properties_size(numprop);
    for (unsigned int i=0; i<numprop; i++)
	{
    	is.read(buffer, sizeof(unsigned int));
	unsigned int prop = atoi(buffer);
	obj.set(prop, i);
	}

    return is;
  }



